package cn.com.dhc.danlu.widget.popup;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.math.BigDecimal;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.base.BasePopupWindow;
import cn.com.hd.mall.web.webservices.entity.response.order.OrderDetailResponse.OrderDetailBean.MerchListBean;

/**
 * author: 李一方(<a href="mailto:liyifang@danlu.com">liyifang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-03-17 14:10
 *
 * <p>
 * 显示价格详情的弹窗
 * </p>
 */
public class MerchPricePopupView extends BasePopupWindow {
    private TextView tvMerchName;
    private TextView tvMerchBarcode;
    private TextView tvAvagePrice;
    private TextView tvMerchCount;
    private TextView tvLimitPrice;
    private TextView tvOriginalPrice;
    private ImageView ivCancel;
    private SimpleDraweeView sdvMerch;
    private MerchListBean childViews;

    public MerchPricePopupView(Context context, MerchListBean merchBean) {
        super(context);

        initViews(context, merchBean);
    }

    private void initViews(Context context, MerchListBean merchBean) {
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setAnimationStyle(R.style.popwin_anim_style);
        setOutsideTouchable(true);

        View popView = LayoutInflater.from(context).inflate(R.layout.popview_merch_price_detail, (ViewGroup) ((Activity) context).getWindow().getDecorView(), false);
        setFocusable(true);
        setContentView(popView);
        initChildView(popView);
        setChildViews(merchBean);
    }

    private void initChildView(View popView) {
        tvMerchName = (TextView) popView.findViewById(R.id.tv_merch_name);
        tvMerchBarcode = (TextView) popView.findViewById(R.id.tv_merch_barcode);
        tvAvagePrice = (TextView) popView.findViewById(R.id.tv_avage_price);
        tvMerchCount = (TextView) popView.findViewById(R.id.tv_merch_count);
        tvLimitPrice = (TextView) popView.findViewById(R.id.tv_limit_price);
        tvOriginalPrice = (TextView) popView.findViewById(R.id.tv_original_price);
        ivCancel = (ImageView) popView.findViewById(R.id.iv_cancel);
        sdvMerch = (SimpleDraweeView) popView.findViewById(R.id.sdv_merch);
    }

    private void setChildViews(MerchListBean merch) {
        sdvMerch.setImageURI(merch.getPicUrl());
        tvMerchName.setText(merch.getMerchName());
        tvMerchBarcode.setText("商品条码：".concat(merch.getMerchBarCode()));

        tvAvagePrice.setText(CommonUtils.fenToYuan(new BigDecimal(merch.getAveragePrice())).toPlainString());
        tvMerchCount.setText(" x ".concat(merch.getMerchCount()).concat(merch.getMerchSpec()));

        int merchCount = Integer.parseInt(merch.getMerchCount());
        int limitCount = Integer.parseInt(merch.getPromotionQuantity());

        if (limitCount >= merchCount) {
            tvOriginalPrice.setText("原    价:" + CommonUtils.fenToYuan(new BigDecimal(merch.getUnitPrice())).toPlainString() + " x 0" + merch.getMerchSpec());
            tvLimitPrice.setText("限购价:" + CommonUtils.fenToYuan(new BigDecimal(merch.getPromotionPrice())).toPlainString() + " x " + merchCount + merch.getMerchSpec());
        } else {
            tvOriginalPrice.setText("原    价:" + CommonUtils.fenToYuan(new BigDecimal(merch.getUnitPrice())).toPlainString() + " x " + (merchCount - limitCount) + merch.getMerchSpec());
            tvLimitPrice.setText("限购价:" + CommonUtils.fenToYuan(new BigDecimal(merch.getPromotionPrice())).toPlainString() + " x " + limitCount + merch.getMerchSpec());
        }

        ivCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
